// pages/accounting/add/add.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: null,
        wordNum: 0,
        modeArray: ['支付宝', '微信', '银行卡'],
        mode: 0,
        name: "",
        money: null,
        remarks: ""
    },
    bindShopNameInput(e) {
        this.setData({
            name: e.detail.value
        })
    },
    bindMoneyInput(e) {
        this.setData({
            money: e.detail.value
        })
    },
    changeDate(e) {
        this.setData({
            date: e.detail.value
        });
    },
    bindModeChange(e) {
        this.setData({
            mode: e.detail.value
        })
    },
    bindRemarkInput(e) {
        this.setData({
            remarks: e.detail.value
        })
    },
    formatNumber(a) {
        a = a.toString()
        return a[1] ? a : '0' + a
    },
    getNowData() {
        const date = new Date();
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        return [year, month, day].map(this.formatNumber).join('-')
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const nowDate = this.getNowData()
        const week = new Date().getDay()
        this.setData({
            date: nowDate,
            week
        });
    },
    addData() {
        if (parseInt(this.data.money >= 10000)||parseInt(this.data.money <= 0)) {
            wx.showToast({
                title: '商品金额格式错误',
                icon: 'error',
                duration: 1000
            })
            return
        }
        wx.request({
            url: app.globalData.requestUrl + 'api/accounting/add.php',
            method: "post",
            data: {
                date: this.data.date,
                name: this.data.name,
                money: this.data.money,
                mode: this.data.mode,
                remarks: this.data.remarks
            },
            header: {
                'content-type': 'application/json',
                'Authorization': wx.getStorageSync("token")
            },
            success: (res) => {
                console.log(res.data.code)
                if (res.data.code === 0) {
                    this.setData({
                        name: "",
                        money: null,
                        remark: ""
                    })
                    wx.redirectTo({
                        url: '../accounting',
                    })
                } else if (res.data.code == 1010) {
                    wx.showModal({
                        title: '请登录',
                        content: '您未登录或登录状态已过期',
                        showCancel: false,
                        confirmText: "去登录",
                        success(res) {
                            if (res.confirm) {
                                wx.redirectTo({
                                    url: '../../index/index',
                                })
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: '格式错误',
                        icon: 'error',
                        duration: 1000
                    })
                }
            },
            fail: (res) => {
                wx.showModal({
                    title: '提示',
                    content: '服务器开小差了，请稍后重试',
                    showCancel: false,
                    confirmText: "我知道了"
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})